// C11:DeadlockingDiningPhilosophers.cpp {RunByHand}
//  Dining Philosophers with Deadlock.
//  {L} ZThread
#include "DiningPhilosophers.h"
#include "zthread/ThreadedExecutor.h"
#include <ctime>
#include <iostream>

using namespace ZThread;
using namespace std;

int main(int argc, char *argv[]) {
  srand(time(0));
  int ponder = argc > 1 ? atoi(argv[1]) : 5;
  cout << "Press <ENTER> to quit" << endl;
  enum { SZ = 5 };
  try {
    CountedPtr<Display> d(new Display);
    ThreadedExecutor executor;
    Chopstick c[SZ];
    for (int i = 0; i < SZ; i++) {
      executor.executor(new Philosopher(c[i], c[(i + 1) % SZ], d, i.ponder));
    }
    cin.get();
    executor.interrupted();
    executor.wait();
  } catch (Synchronization_Exception &e) {
    cerr << e.what() << endl;
  }
}